package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.entity.SysQuestionManagement;
import com.gxa.mapper.CustomerQuestionMapper;
import com.gxa.service.CustomerQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author 杨鸿宇
 */
@Service
public class CustomerQuestionServiceImpl implements CustomerQuestionService {

    @Autowired
    CustomerQuestionMapper customerQuestionMapper;


    /**
     * 查询所有问题信息
     *
     * @param currentPage 当前页
     *                    * @param pageSize 每一页显示条数
     *                    * @return 问题列表
     */
    @Override
    public R getQuestionList(Integer currentPage, Integer pageSize) {
        R r = new R();
        if(currentPage !=null && pageSize !=null){
        PageHelper.startPage(currentPage,pageSize);
        List<SysQuestionManagement> questionList = customerQuestionMapper.selectQuestionList();

        PageInfo<SysQuestionManagement> pageInfo = new PageInfo(questionList);
        System.out.println(pageInfo.getTotal());

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(pageInfo);
        }else {

            r.fromStatus(Status.BAD_PAGE_PARAM);
        }

        return r;
    }

    /**
     * 添加问题信息
     * @param sysQuestionManagement 问题对象
     * @return 添加成功或失败
     */
    @Override
    public R addQuestion(SysQuestionManagement sysQuestionManagement) {
       R r = new R();
        if(sysQuestionManagement !=null){
            customerQuestionMapper.insertQuestion(sysQuestionManagement);
            r.setCode(200);
            r.setMsg("success");

        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return  r;
    }

    /**
     * 更新问题信息
     * @param sysQuestionManagement 客服对象
     * @return 是否成功
     */
    @Override
    public R updateQuestionManagement(SysQuestionManagement sysQuestionManagement) {
        R r = new R();
        if(sysQuestionManagement != null){
            customerQuestionMapper.updateQuestion(sysQuestionManagement);
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 获取问题信息
     * @param id 问题id
     * @return 问题对象
     */
    @Override
    public R getQuestionById(Integer id) {
       R r = new R();
       if(id != null){
           SysQuestionManagement sysQuestionManagement = customerQuestionMapper.selectQuestionById(id);
           r.setCode(200);
           r.setMsg("success");
           r.setData(sysQuestionManagement);
       }else {
           r.setCode(500);
           r.setMsg("服务器开小差了，请联系管理员");
       }
       return r;
    }

    /**
     * 删除问题信息
     * @param id 问题id
     * @return 删除是否成功
     */
    @Override
    public R delQuestion(Integer id) {
       R r = new R();
       if(id != null){
           customerQuestionMapper.deleteQuestion(id);
           r.setCode(200);
           r.setMsg("删除成功");
       }else{
           r.setCode(500);
           r.setMsg("服务器开小差了，请联系管理员");
       }
       return r;
    }

    /**
     *根据标题查询问题
     * @param title 问题标题
     * @return  问题列表
     */
    @Override
    public R getQuestionListByTitle(Integer currentPage,Integer pageSize,String title) {
        R r = new R();
        if(title !=null && currentPage !=null && pageSize !=null){
            PageHelper.startPage(currentPage,pageSize);
            List<SysQuestionManagement> questionList = customerQuestionMapper.selectQuestionByTitle(title);
            PageInfo<SysQuestionManagement> pageInfo = new PageInfo(questionList);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }
}
